import {GameVar} from "../../game/GameVar";
import * as proto from "../../socket/proto/proto";
import {SocketUtils} from "../../socket/SocketUtils";
import {wxUtils} from "../../wx/wxUtils";

export class SocketApiUtils {
  private logicSocket = SocketUtils.getLogicSocket();

  gameEnd(score: number, rank: number, kill: number, time: number, bekill: number) {
    const msg = new proto.GameEnd();
    msg.type = GameVar.mode;
    msg.kill = kill;
    msg.score = score;
    msg.rank = rank;
    msg.bekill = bekill
    msg.time = time
    this.logicSocket.emit(proto.CommandProtocol.GAME_END, msg)
  }

  gmCmd(cid: number, num: number, type: number) {
    const msg = new proto.ItemOp()
    msg.type = type
    msg.item = {cid, num}
    this.logicSocket.emit(proto.CommandProtocol.GM, msg)
  }

  buyItem(cid: number) {
    this.logicSocket.emit(proto.CommandProtocol.BUY_REQ, {cid})
  }

  mergeItem(cid: number) {
    this.logicSocket.emit(proto.CommandProtocol.MERGE, {cid})
  }

  useItem(id: number, type: number) {
    this.logicSocket.emit(proto.CommandProtocol.CHANGE_SKIN, {id, type});
  }

  signSevenLogin() {
    this.logicSocket.emit(proto.CommandProtocol.SEVEN_LOGIN, {});
  }

  getNewbieGift() {
    this.logicSocket.emit(proto.CommandProtocol.ROOKIE_GIFT, {});
  }

  shareNotify(type = 1, reward = 0) {
    // 发起分享时通知服务器(新手礼包，发起邀请就可完成)
    this.logicSocket.emit(proto.CommandProtocol.SHARE, {type, reward});
  }

  refreshDailyGift() {
    // 打开每日礼包时刷新状态(状态改变没有主动推送)
    this.logicSocket.emit(proto.CommandProtocol.EveryDayGift_REQ, {type: 1});
  }

  getDailyGift() {
    this.logicSocket.emit(proto.CommandProtocol.EveryDayGift_REQ, {type: 2});
  }

  sign() {
    this.logicSocket.emit(proto.CommandProtocol.SIGN, {});
  }

  reqRankAll(): Promise<proto.GroupRank> {
    this.logicSocket.sendBasicCmd(proto.CommandProtocol.Rank_Req);
    return new Promise<proto.GroupRank>((resolve, reject) => {
      this.logicSocket.once(proto.GroupRank, function (data: proto.GroupRank) {
        resolve(data)
      })
    })
  }

  reqInviteState(): Promise<proto.InviteStateRsp> {
    this.logicSocket.emit(proto.CommandProtocol.InviteState_Req, {type: -1})
    return new Promise<proto.InviteStateRsp>((resolve, reject) => {
      this.logicSocket.once(proto.InviteStateRsp, function (data: proto.InviteStateRsp) {
        resolve(data)
      })
    })
  }

  getInviteReward(index: number) {
    this.logicSocket.emit(proto.CommandProtocol.InviteState_Req, {type: index})
    return new Promise((resolve) => {
      this.logicSocket.once(proto.CommonResult, function (data: proto.CommonResult) {
        resolve(data.code)
      })
    })
  }

  //1金币单次，2钻石单次，3钻石十连
  luckyTurntable(type: number): Promise<proto.FortuneWheelRsp> {
    this.logicSocket.emit(proto.CommandProtocol.TurnTable_REQ, {type})
    return new Promise<proto.FortuneWheelRsp>((resolve, reject) => {
      this.logicSocket.once(proto.FortuneWheelRsp, function (data: proto.FortuneWheelRsp) {
        if (data.code === 0) {
          resolve(data)
        } else {
          reject(data.errorCode)
        }
      })
    })
  }

  reqAchievement(): Promise<proto.AchievementRsp> {
    this.logicSocket.emit(proto.CommandProtocol.Achievement_Req, {type: 0})
    return new Promise((resolve => {
      this.logicSocket.once(proto.AchievementRsp, function (data: proto.AchievementRsp) {
        resolve(data)
      })
    }))
  }

  reqAchievementReward(id: number): Promise<proto.CommonResult> {
    this.logicSocket.emit(proto.CommandProtocol.Achievement_Req, {type: id})
    return new Promise((resolve => {
      this.logicSocket.once(proto.CommonResult, function (data: proto.CommonResult) {
        resolve(data)
      })
    }))
  }

  uploadUserInfo(postData) {
    const {uid, openid, nick, inviteId} = postData;
    const data = {uid, openid, nick: GameVar.userInfo.nick, inviteId, headUrl: GameVar.userInfo.avatarUrl, channel: wxUtils.getChannel()}
    for (let k in data) {
      if (!data[k]) {
        delete data[k]
      }
    }
    this.logicSocket.emit(proto.CommandProtocol.USER_GAME_INFO, data)
  }

  prepay(id: number): Promise<proto.PayData> {
    return new Promise((resolve, reject) => {
      const msg = new proto.Pay();
      msg.id = id;
      this.logicSocket.emit(proto.CommandProtocol.Pay_Req, msg)
      this.logicSocket.once(proto.PayData, (data) => {
        resolve(data.toJSON())
      })
    })
  }
}

export class TestApiUtils {

  gameEnd(score: number, rank: number, kill: number) {

  }

  gmCmd(cid: number, num: number, type: number) {
  }

  buyItem(cid: number) {
  }

  mergeItem(cid: number) {
  }

  useItem(id: number, type: number) {
  }

  signSevenLogin() {
  }

  getNewbieGift() {
  }

  shareNotify() {
  }
}

export const apiUtils: SocketApiUtils = new SocketApiUtils();
window['apiUtils'] = apiUtils;